package com.cheer.vo;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.cheer.entity.*;
import com.cheer.enums.DocTypeEnum;
import com.cheer.enums.OrgTypeEnum;
import com.cheer.enums.ReviewedStatusEnum;
import com.cheer.enums.TitleEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Accessors(chain = true)
public class StandingBookVO {

    private String applicantName;

    private Integer title;

    private Boolean awarded;

    private String phoneAccount;

    private String orgName;

    private Integer registerStatus;

    private String contactPerson;

    private String contactPersonPhone;

    private String contactPersonPost;

    private String contactPersonBirth;

    private Integer contactPersonAge;

    private String phone;

    private String address;

    private Integer personNumber;

    private Integer youthNumber;

    private String youthPercent;

    private String achievement;

    private String award;

    private Boolean reviewTypeEnabled;

    private Integer reviewType;

    private Integer reviewStatus;

    private Long joinReviewOrgId;

    private Long[] joinReviewOrgPath;

    private DocumentVO memberDoc;

    private Integer starNum;

    @ApiModelProperty("是否允许点击申请")
    private Boolean isAllowedApply;

    private List<DocumentVO> fileDocs;

    public static StandingBookVO build(Account account, Applicant applicant, Organization organization, Organization joinOrg,
                                       StandingBook standingBook, List<StandingBookDocument> documents,
                                       String resourceUrl) {
        StandingBookVO standingBookVO = new StandingBookVO();
        standingBookVO.setApplicantName(applicant.getApplicantName());
        standingBookVO.setPhoneAccount(account.getPhone());
        standingBookVO.setOrgName(organization.getOrgName());
        standingBookVO.setRegisterStatus(applicant.getRegisterStatus());
        standingBookVO.setReviewType(applicant.getReviewType());
        if (joinOrg != null) {
            standingBookVO.setJoinReviewOrgId(joinOrg.getId());
            standingBookVO.setJoinReviewOrgPath(Arrays.stream(StringUtils.split(joinOrg.getOrgPath(), ","))
                    .skip(2)
                    .map(Long::valueOf)
                    .toArray(Long[]::new));
        }
        standingBookVO.setJoinReviewOrgId(applicant.getJoinReviewOrgId());
        standingBookVO.setContactPerson(applicant.getContactPerson());
        standingBookVO.setContactPersonPhone(applicant.getContactPersonPhone());
        standingBookVO.setContactPersonPost(applicant.getContactPersonPost());
        standingBookVO.setContactPersonBirth(DateUtil.format(applicant.getContactPersonBirth(), DatePattern.NORM_DATETIME_FORMAT));
        standingBookVO.setContactPersonAge(DateUtil.ageOfNow(applicant.getContactPersonBirth()));
        standingBookVO.setPersonNumber(applicant.getPersonNumber());
        standingBookVO.setStarNum(applicant.getStarNum());
        standingBookVO.setYouthNumber(applicant.getYouthNumber());
        NumberFormat nt = NumberFormat.getPercentInstance();
        if(applicant.getYouthNumber() == 0){
            standingBookVO.setYouthPercent(nt.format(0));
        }else{
            standingBookVO.setYouthPercent(nt.format(1.0 * applicant.getYouthNumber() /
                    applicant.getPersonNumber()));
        }
        standingBookVO.setPhone(applicant.getPhone());
        standingBookVO.setAddress(applicant.getAddress());
        standingBookVO.setReviewTypeEnabled(organization.getOrgType() == OrgTypeEnum.INDUSTRY.getCode());
        // 荣誉称号处理
        if (applicant.getTitle() == TitleEnum.NO.getType()) {
            standingBookVO.setTitle(applicant.getRegisterTitle());
            standingBookVO.setAwarded(false);
        } else {
            standingBookVO.setTitle(applicant.getTitle());
            standingBookVO.setAwarded(true);
        }
        // 台账处理
        if (standingBook != null) {
            standingBookVO.setAchievement(standingBook.getAchievement());
            standingBookVO.setAward(standingBook.getAward());
            standingBookVO.setReviewStatus(standingBook.getReviewStatus());
        } else {
            standingBookVO.setReviewStatus(ReviewedStatusEnum.TO_REVIEW.getCode());
        }
        // 上传文件处理
        documents.stream()
                .filter(doc -> doc.getDocType() == DocTypeEnum.MEMBER.getCode())
                .findFirst()
                .ifPresent(document -> standingBookVO.setMemberDoc(DocumentVO.build(document, resourceUrl)));
        List<StandingBookDocument> fileDocs = documents.stream()
                .filter(doc -> doc.getDocType() == DocTypeEnum.FILE.getCode())
                .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(fileDocs)) {
            standingBookVO.setFileDocs(fileDocs.stream()
                    .map(fileDoc -> DocumentVO.build(fileDoc, resourceUrl))
                    .collect(Collectors.toList()));
        }

        // 获取当前年份比对
        String currentYear = DateUtil.format(new Date(), "yyyy");
        String vintage = applicant.getVintage();
        if(StrUtil.isEmpty(vintage) ||
                applicant.getVintage().equals(currentYear) == false){
            standingBookVO.setIsAllowedApply(true);
        }else{
            standingBookVO.setIsAllowedApply(false);
        }
        return standingBookVO;
    }


}
